package com.yl.paymanagement.controller;

import com.yl.paymanagement.config.PayConfig;
import com.yl.paymanagement.pojo.RechargeRecoding;
import com.yl.paymanagement.pojo.ResultEntity;
import com.yl.paymanagement.pojo.YlPayModel;
import com.yl.paymanagement.service.AlipayService;
import com.yl.paymanagement.service.RechargeRecodingService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
/*@CrossOrigin*/
@RequestMapping("/paymanagement")

public class PayController {

    @Autowired
    AlipayService alipayService;

    @Autowired
    RechargeRecodingService rechargeRecodingService;

    @Autowired
    RabbitTemplate rabbitTemplate;


    /*
    * 支付宝充值接口
    * */
    @PostMapping ("/aliRechargePay")
    public ResultEntity<String> alipayRecharge(@RequestBody YlPayModel ylPayModel) throws Exception {
        return new ResultEntity<String>(200,"成功",alipayService.alipayRecharge(ylPayModel));
    }






    @PostMapping ("/cardPay")
    public ResultEntity<String> cardRechargeOrder(@RequestBody YlPayModel ylPayModel) throws Exception {
        System.out.println("card");
        return new ResultEntity<String>(200,"成功",alipayService.cardPay(ylPayModel));
    }


    /*
    *
    * 其他商品支付接口
    * */
    @PostMapping ("/aliPayOther")
    public ResultEntity<String> aliPayOther(@RequestBody YlPayModel ylPayModel) throws Exception {


        return new ResultEntity<String>(200,"成功",alipayService.aliPayOther(ylPayModel));
    }



    /*
    *
    * 充值成功后,支付宝回调接口
    *
    * */
    @RequestMapping("/notifyUrl")
    public void notifyUrl(HttpServletRequest request,HttpServletResponse httpServletResponse) throws IOException {

            String out_trade_no = new String(request.getParameter("out_trade_no"));
            // 交易状态
            String trade_status = new String(request.getParameter("trade_status"));
            String subject = new String(request.getParameter("subject"));
            if (trade_status.equals("TRADE_SUCCESS")) {//支付成功商家操作

                if(subject.contains("充值")){
                    rechargeRecodingService.updataStatus(out_trade_no);
                }else{

                }
            }
    }

    @RequestMapping("/returnUrl")
    public void returnUrl(HttpServletRequest request, HttpServletResponse httpServletResponse) throws Exception {
        httpServletResponse.sendRedirect("http://localhost:80/#/index");
    }




}
